import time
import sys
import uiautomator2 as u2

from component.Common import Common


class Opt:
    """uiautomator2包装对象，目的：提供更多的操作可能性"""

    def __init__(self, device_host_port):
        print(f"开始连接设备{device_host_port}")
        try:
            self.d = u2.connect(device_host_port)
            print("设备连接成功")
        except Exception as e:
            print(f"无法连接设备，错误原因：{e}")
            sys.exit(1)

    #关闭应用
    def close_app(self):
        # 关闭app
        self.d.app_stop('行政版')
        # 清楚app的数据
        self.d.app_clear('行政版')
    # 中断
    def exit(self, exit_desc="未指定中断原因..."):
        self.pt(exit_desc)
        sys.exit(1)

    # 返回首页
    def home(self, sleep_time):
        self.d.press("home")
        time.sleep(sleep_time)

    def obtain_element(self, element, element_val):
        return self.__element_obj(element, element_val)

    # 检查text元素是否存在
    def obtain_element_text(self, text):
        return self.obtain_element('text', text)

    # 检查class_name元素是否存在
    def obtain_element_class_name(self, class_name):
        return self.obtain_element('className', class_name)

    # 检查resource元素是否存在
    def obtain_element_resource(self, resource_id):
        return self.obtain_element('resourceId', resource_id)

    # 检查元素受否存在
    def exists(self, element, element_val):
        return self.__element_obj(element, element_val).exists

    # 检查text元素是否存在
    def exists_text(self, text):
        return self.obtain_element('text', text).exists

    # 检查class_name元素是否存在
    def exists_class_name(self, class_name):
        return self.obtain_element('className', class_name).exists

    # 检查resource元素是否存在
    def exists_resource(self, resource_id):
        return self.obtain_element('resourceId', resource_id).exists

    # 点击className元素
    def click(self, element, element_val, sleep_time=0, log_text="未指定描述..."):
        self.__element_obj(element, element_val).click()
        self.sleep_pt(sleep_time, log_text)

    # 点击className元素
    def click_class_name(self, class_name, sleep_time=0, log_text="未指定描述..."):
        self.obtain_element('className', class_name).click()
        self.sleep_pt(sleep_time, log_text)

    # 点击text元素
    def click_text(self, text, sleep_time=0, log_text="未指定描述..."):
        self.obtain_element('text', text).click()
        self.sleep_pt(sleep_time, log_text)

    # 点击resourceId元素
    def click_resource(self, resource_id, sleep_time=0, log_text="未指定描述..."):
        self.obtain_element('resourceId', resource_id).click()
        self.sleep_pt(sleep_time, log_text)

    # 点击坐标元素
    def click_bounds(self, x1, y1, x2, y2, sleep_time, log_text):
        c_x, c_y = Common().bounds_x_y(x1, y1, x2, y2)
        self.d.click(c_x, c_y)
        self.sleep_pt(sleep_time, log_text)

    # 等待及打印
    def sleep_pt(self, sleep_time, log_text="未指定描述..."):
        self.pt(log_text)
        time.sleep(sleep_time)

    # 打印
    def pt(self, text):
        print(text)

    # 操作元素对象
    def __element_obj(self, element, element_val):
        if element == 'resourceId':
            return self.d(resourceId=element_val)
        if element == 'text':
            return self.d(text=element_val)
        if element == 'className':
            return self.d(className=element_val)
        if element == 'contentDesc':
            return self.d(contentDesc=element_val)
